# -*- coding: utf-8 -*-
"""
 @Time    : 2021/1/18 下午4:48
 @Author  : tgy

 hashable
 unhashable

 保持顺序
"""


# set集合中只能存放hashable类型，如: str,int,tuple, 不能存放list/dict
def remove_dup(lists):
    ...
    container = set()
    for li in lists:
        if li not in container:
            yield li
            container.add(li)


# 构建一个hashable对象来判断唯一性，下面使用的是tuple
def remove_unhashable_dup(lists,key=None):
    ...
    container = set()
    for li in lists:
        ...
        val = li if key is None else key(li)
        if val not in container:
            yield li
            container.add(val)



if __name__ == '__main__':
    ...
    s = set()
    # s.add({'1':'2'})
    # print(list(remove_dup([1, 3, 1, 2, 4, 2])))
    persons = [{'x':1,'y':2},{'x':1,'y':2}]
    print(list(remove_unhashable_dup(persons, key=lambda x:(x['x'],x['y']))))
